package cn.jiangxiong.pethome.system.dept.service.impl;

import cn.jiangxiong.pethome.base.service.impl.BaseServiceImpl;
import cn.jiangxiong.pethome.system.dept.domain.Department;
import cn.jiangxiong.pethome.system.dept.mapper.DepartmentMapper;
import cn.jiangxiong.pethome.system.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true , propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

/*

    @Override
    public Department findOne(Long id) {
        return departmentMapper.findOne(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public List<Department> findByQuery(DepartmentQuery query) {
        return departmentMapper.findByQuery(query);
    }

    @Override
    public Long findByQueryCount(DepartmentQuery query) {
        return departmentMapper.findByQueryCount(query);
    }
*/


    @Transactional
    @Override
    public void add(Department department) {
         //优化代码。
        departmentMapper.add(department);
        buildDirPath(department);
       /* //没有父级的情况
        if(department.getParent_id() == null){
           *//* departmentMapper.add(department);*//*
           //由新增的SQL语句中返回的主键id得到该部门的id值
            Long id = department.getId();
            //由于没有父级，所以该条数据的parent_id为该数据的id
            department.setDirPath("/"+id);
            //修改这条新增的数据把parent_id值修改进去
            //departmentMapper.update(department);
        }else{//有父级的情况
            //获取父类的id
            Long parentId = department.getParent_id();
            //由父类的id查询到父类的所有信息
             Department parent= departmentMapper.findOne(parentId);
             //得到父类数据中的Path值
             String dirPath = parent.getDirPath();
             //保存该条新增数据
            // departmentMapper.add(department);
             //由获取到的父级数据的dirPath拼接新增数据的返回主键id修改到新增数据的dirPath中
             department.setDirPath((dirPath+"/"+department.getId()));
             //departmentMapper.update(department);

        }
        //优化代码，提出公共代码*/
        departmentMapper.update(department);
           
    }

    /**
     * 构建dirPath的路径
     * @param department
     */
    //抽出公共方法,以后有其他类使用该方法时把私有该为公共
    private void buildDirPath(Department department){
        if(department.getParent_id() == null){
            //由新增的SQL语句中返回的主键id得到该部门的id值
            Long id = department.getId();
            //由于没有父级，所以该条数据的parent_id为该数据的id
            department.setDirPath("/"+id);
        }else{//有父级的情况
            //获取父类的id
            Long parentId = department.getParent_id();
            //由父类的id查询到父类的所有信息
            Department parent= departmentMapper.findOne(parentId);
            //得到父类数据中的Path值
            String dirPath = parent.getDirPath();
            //由获取到的父级数据的dirPath拼接新增数据的返回主键id修改到新增数据的dirPath中
            department.setDirPath((dirPath+"/"+department.getId()));
        }

    }





    @Transactional
    @Override
    public void update(Department department) {
        /*if(department.getParent_id() == null){
            //由新增的SQL语句中返回的主键id得到该部门的id值
            Long id = department.getId();
            //由于没有父级，所以该条数据的parent_id为该数据的id
            department.setDirPath("/"+id);
        }else{//有父级的情况
            //获取父类的id
            Long parentId = department.getParent_id();
            //由父类的id查询到父类的所有信息
            Department parent= departmentMapper.findOne(parentId);
            //得到父类数据中的Path值
            String dirPath = parent.getDirPath();
            //由获取到的父级数据的dirPath拼接新增数据的返回主键id修改到新增数据的dirPath中
            department.setDirPath((dirPath+"/"+department.getId()));
        }*/
        if (department.getId().equals(department.getParent_id())){
            throw new RuntimeException("无法设置自己为自己上级部门");
        }
        //优化代码，调用提出的方法
        buildDirPath(department);
        departmentMapper.update(department);

    }
    @Transactional
    @Override
    public void delete(Long id) {
        //有下级部门时不能删除
        Long  count = departmentMapper.findByParentId(id);
        if (count == 0){
            departmentMapper.delete(id);

        }else {
            throw new RuntimeException("当前部门存在下级不能直接删除");
        }

    }
    /*@Transactional
    @Override
    public void batchDel(List<Long> ids) {
        departmentMapper.batchDel(ids);
    }*/

    @Override
    public List<Department> deptTree() {
        //第一步查询全部
        List<Department> departments = departmentMapper.findAll();

        //第三步需要建立好关系网 ，由子类找出父类
        for (Department department: departments){
            //找最顶级
            if (department.getParent_id() !=null){
                for (Department parent : departments){
                    //判断是不是父类
                    if (department.getParent_id() == parent.getId()){
                         List<Department> children = parent.getChildren();
                         children.add(department);
                         break;
                    }
                }
            }

        }
        //第二步找到最顶级了
        //在filter步骤中过滤出父类id为null
        List<Department> firstTree = departments.stream()
                .filter(department -> department.getParent_id() == null)
                .collect(Collectors.toList());//转为list
        return firstTree;
    }
}
